package cn.kgc.controller;

import cn.kgc.entity.AQIndex;
import cn.kgc.entity.District;
import cn.kgc.service.DistrictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class DistrictController {
    @Resource
    private DistrictService districtService;
    @Resource
    private District district;

    @RequestMapping(value = "/selectDisAndAQ")
    public String selectDisAndAQ(
            @RequestParam(value = "pagenum",defaultValue = "1") int pagenum ,
            @RequestParam(value = "name",defaultValue = "不限") String name,
            Model model){
        PageHelper.startPage(pagenum,3);
        if("不限".equals(name)){
            name = null;
        }
        List<District> districtList = districtService.selectDisAndAQ(name);
        for (District district : districtList) {
            System.out.println(district);
        }
        PageInfo<District> pageInfo = new PageInfo<>(districtList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",name);
        return "index";
    }

    @RequestMapping("/addAQ")
    public String addAQIndex(AQIndex aqIndex){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String lastDate = simpleDateFormat.format(date);
        aqIndex.setLastModifyTime(lastDate);
        int result = districtService.addAQ(aqIndex);
        return "redirect:selectDisAndAQ";
    }

    @RequestMapping("/findAQ")
    public String findAQ(int pm10,Model model){
        AQIndex aqIndex = districtService.findBYID(pm10);
        model.addAttribute("aqIndex",aqIndex);
        return "update";
    }

    @RequestMapping("/updateAQ")
    public String updateAQ(AQIndex aqIndex){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String lastDate = simpleDateFormat.format(date);
        aqIndex.setLastModifyTime(lastDate);
        int result = districtService.updateAQ(aqIndex);
        return "redirect:selectDisAndAQ";
    }
}
